package GoodsActivityService

import (
	"context"
	toolsDb "gdshop-back-end-go/common/db"
	"gdshop-back-end-go/common/entity"
)

func GetGoodsActivityById(ctx context.Context, id int) (*entity.GoodsActivity, error) {
	// 找到活动时间内的活动
	var tmpModel *entity.GoodsActivity
	err := toolsDb.GetUnSafaTable(ctx, "goods_activity").Where(
		"ga.delete_at<1 AND ga.status = ? AND (ga.start_time <= UNIX_TIMESTAMP(NOW()) AND ga.end_time >= UNIX_TIMESTAMP(NOW()))",
		1,
	).Where("id", id).As("ga").LeftJoin("goods_activity_rule gar", "gar.activity_id = ga.id").
		Fields("ga.*,gar.rules,gar.loop").Order("ga.sort DESC,ga.id ASC").
		Scan(&tmpModel)
	if err != nil {
		return nil, err
	}

	return tmpModel, nil
}
